﻿using System;
using System.Collections.Generic;
using System.Text;
using CLoggerNamespace;

namespace HL7v_2x
{
   public class HL7SegmentPID :  IHL7Segment
    {
        
       #region DESARMAR SEGMENTO EN CAMPOS
        public List<HL7Fields> DesarmarSegmentoEnCampos(string strSegment)
          {                         
              //EJEMPLO MENSAJE HL7
              /*            
                 PID|1||163136^^^^HIG|24957389^^^^DNI|LANE^LUISA||19751128000000|F|||BLANCO ENCALADA 3422 4 C^^||||||||24957389 <cr>   
              */
             List<HL7Fields> lstHL7Fields = new List<HL7Fields>();
             string[] strArrAux = new string[] { };

             try
             {
                 strArrAux = strSegment.Split('|');
                 int inCantBarras = strArrAux.Length - 1;
                 CLogger.Append("Cantidad de Barras: " + inCantBarras + " - MAXIMO DE 37 CAMPOS.", CLogger.INFO);
             }
             catch (Exception ex)
             {
                 CLogger.Append("ERROR PARCEANDO LAS BARRAS | Ex: " + ex.Message, CLogger.INFO);
                 throw ex;
             }


             try
             {
                 if ((strArrAux.Length > 0) &&(strArrAux[0] != null) && (strArrAux[0] != ""))    // SetId_PID = 00104,
                 {
                     CLogger.Append("Field 0: " + strArrAux[0], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.SetId_PID, enc.GetBytes(strArrAux[0])));
                 }
                 if ((strArrAux.Length > 1)&&(strArrAux[1] != null) && (strArrAux[1] != null))  // Patient_id_PID = 00105,
                 {
                     CLogger.Append("Field 1: " + strArrAux[1], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.Patient_id_PID, enc.GetBytes(strArrAux[1])));
                 }
                 if ((strArrAux.Length > 2)&&(strArrAux[2] != "") && (strArrAux[2] != null)) // PatientIdentifierList_PID = 00106,
                 {
                     CLogger.Append("Field 2: " + strArrAux[2], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.PatientIdentifierList_PID, enc.GetBytes(strArrAux[2])));
                 }
                 if ((strArrAux.Length > 3)&&(strArrAux[3] != "") && (strArrAux[3] != null)) // AlternatePatientID_PID = 00107,
                 {
                     CLogger.Append("Field 3: " + strArrAux[3], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.AlternatePatientID_PID, enc.GetBytes(strArrAux[3])));
                 }
                 if ((strArrAux.Length > 4) && (strArrAux[4] != "") && (strArrAux[4] != null)) //   PatientName_PID = 00108,
                 {
                     CLogger.Append("Field 4: " + strArrAux[4], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.PatientName_PID, enc.GetBytes(strArrAux[4])));
                 }
                 if ((strArrAux.Length > 5) && (strArrAux[5] != "") && (strArrAux[5] != null)) //  MotherMaidenName_PID_NK1 = 00109,
                 {
                     CLogger.Append("Field 5: " + strArrAux[5], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.MotherMaidenName_PID_NK1, enc.GetBytes(strArrAux[5])));
                 }
                 if ((strArrAux.Length > 6) && (strArrAux[6] != "") && (strArrAux[6] != null)) //DateTimeofBirth_PID_NK1 = 00110,
                 {
                     CLogger.Append("Field 6: " + strArrAux[6], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.DateTimeofBirth_PID_NK1, enc.GetBytes(strArrAux[6])));
                 }
                 if ((strArrAux.Length > 7) && (strArrAux[7] != "") && (strArrAux[7] != null))     // AdministrativeSex_PID_NK1 = 00111,
                 {
                     CLogger.Append("Field 6: " + strArrAux[7], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.AdministrativeSex_PID_NK1, enc.GetBytes(strArrAux[7])));
                 }
                 if ((strArrAux.Length > 8) && (strArrAux[8] != "") && (strArrAux[8] != null))     //  PatientAlias_PID = 00112,
                 {
                     CLogger.Append("Field 8: " + strArrAux[8], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.PatientAlias_PID, enc.GetBytes(strArrAux[8])));
                 }
                 if ((strArrAux.Length > 9) && (strArrAux[9] != "") && (strArrAux[9] != null))     //  Race_PID_NK1 = 00113,
                 {
                     CLogger.Append("Field 9: " + strArrAux[9], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.Race_PID_NK1, enc.GetBytes(strArrAux[9])));
                 }
                 if ((strArrAux.Length > 10) && (strArrAux[10] != "") && (strArrAux[10] != null))     //  PatientAddress_PID = 00114,
                 {
                     CLogger.Append("Field 10: " + strArrAux[10], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.PatientAddress_PID, enc.GetBytes(strArrAux[10])));
                 }
                 if ((strArrAux.Length > 11) && (strArrAux[11] != "") && (strArrAux[11] != null))     //  CountyCode_PID = 00115,
                 {
                     CLogger.Append("Field 11: " + strArrAux[11], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.CountyCode_PID, enc.GetBytes(strArrAux[11])));
                 }
                 if ((strArrAux.Length > 12) && (strArrAux[12] != "") && (strArrAux[12] != null))     //  PhoneNumberHouse_PID = 00116,
                 {
                     CLogger.Append("Field 12: " + strArrAux[12], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.PhoneNumberHouse_PID, enc.GetBytes(strArrAux[12])));
                 }
                 if ((strArrAux.Length > 13) && (strArrAux[13] != "") && (strArrAux[13] != null))     //  PhoneNumberBusiness_PID = 00117,
                 {
                     CLogger.Append("Field 13: " + strArrAux[13], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.PhoneNumberBusiness_PID, enc.GetBytes(strArrAux[13])));
                 }
                 if ((strArrAux.Length > 14) && (strArrAux[14] != "") && (strArrAux[14] != null))     //  PrimaryLanguage_PID_NK1 = 00118,
                 {
                     CLogger.Append("Field 14: " + strArrAux[14], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.PrimaryLanguage_PID_NK1, enc.GetBytes(strArrAux[14])));
                 }
                 if ((strArrAux.Length > 15) && (strArrAux[15] != "") && (strArrAux[15] != null))     //  MaritalStatus_PID_NK1 = 00119,
                 {
                     CLogger.Append("Field 15: " + strArrAux[15], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.MaritalStatus_PID_NK1, enc.GetBytes(strArrAux[15])));
                 }
                 if ((strArrAux.Length > 16) && (strArrAux[16] != "") && (strArrAux[16] != null))     //  Religion_PID_NK1 = 00120,
                 {
                     CLogger.Append("Field 16: " + strArrAux[16], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.Religion_PID_NK1, enc.GetBytes(strArrAux[16])));
                 }
                 if ((strArrAux.Length > 17) && (strArrAux[17] != "") && (strArrAux[17] != null))     // PatientAccountNumber_PID = 00121,
                 {
                     CLogger.Append("Field 17: " + strArrAux[17], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.PatientAccountNumber_PID, enc.GetBytes(strArrAux[17])));
                 }
                 if ((strArrAux.Length > 18) && (strArrAux[18] != "") && (strArrAux[18] != null))     //  SSNNumberPatient_PID = 00122,
                 {
                     CLogger.Append("Field 18: " + strArrAux[18], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.SSNNumberPatient_PID, enc.GetBytes(strArrAux[18])));
                 }

                 if ((strArrAux.Length > 19) && (strArrAux[19] != "") && (strArrAux[19] != null))     //  DriverLicenseNumberPatient_PID = 00123,
                 {
                     CLogger.Append("Field 19: " + strArrAux[19], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.DriverLicenseNumberPatient_PID, enc.GetBytes(strArrAux[19])));
                 }
                 if ((strArrAux.Length > 20) && (strArrAux[20] != "") && (strArrAux[20] != null))     //  MotherIdentifier_PID=00124,
                 {
                     CLogger.Append("Field 20: " + strArrAux[20], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.MotherIdentifier_PID, enc.GetBytes(strArrAux[20])));
                 }
                 if ((strArrAux.Length > 21) && (strArrAux[21] != "") && (strArrAux[21] != null))     //   EthnicGroup_PID_NK1=00125,
                 {
                     CLogger.Append("Field 21: " + strArrAux[21], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.EthnicGroup_PID_NK1, enc.GetBytes(strArrAux[21])));
                 }
                 if ((strArrAux.Length > 22) && (strArrAux[22] != "") && (strArrAux[22] != null))     //  BirthPlace_PID=00126,
                 {
                     CLogger.Append("Field 22: " + strArrAux[22], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.BirthPlace_PID, enc.GetBytes(strArrAux[22])));
                 }
                 if ((strArrAux.Length > 23) && (strArrAux[23] != "") && (strArrAux[23] != null))     // MultipleBirthIndicator_PID=00127,
                 {
                     CLogger.Append("Field 23: " + strArrAux[23], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.MultipleBirthIndicator_PID, enc.GetBytes(strArrAux[23])));
                 }
                 if ((strArrAux.Length > 24) && (strArrAux[24] != "") && (strArrAux[24] != null))     //   BirthOrder_PID=00128,
                 {
                     CLogger.Append("Field 24: " + strArrAux[24], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.BirthOrder_PID, enc.GetBytes(strArrAux[24])));
                 }

                 if ((strArrAux.Length > 25) && (strArrAux[25] != "") && (strArrAux[25] != null))     //  Citizenship_PID_NK1=00129,
                 {
                     CLogger.Append("Field 25: " + strArrAux[25], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.Citizenship_PID_NK1, enc.GetBytes(strArrAux[25])));
                 }
                 if ((strArrAux.Length > 26) && (strArrAux[26] != "") && (strArrAux[26] != null))     //   VeteransMilitaryStatus_PID=00130,
                 {
                     CLogger.Append("Field 26: " + strArrAux[26], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.VeteransMilitaryStatus_PID, enc.GetBytes(strArrAux[26])));
                 }
                 if ((strArrAux.Length > 27) && (strArrAux[27] != "") && (strArrAux[27] != null))     //  Nationality_PID_NK1=00739,
                 {
                     CLogger.Append("Field 27: " + strArrAux[27], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.Nationality_PID_NK1, enc.GetBytes(strArrAux[27])));
                 }
                 if ((strArrAux.Length > 28) && (strArrAux[28] != "") && (strArrAux[28] != null))     //   PatientDeathDateandTime_PID=00740,
                 {
                     CLogger.Append("Field 28: " + strArrAux[28], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.PatientDeathDateandTime_PID, enc.GetBytes(strArrAux[28])));
                 }

                 if ((strArrAux.Length > 29) && (strArrAux[29] != "") && (strArrAux[29] != null))     // PatientDeathIndicator_PID=00741,
                 {
                     CLogger.Append("Field 29: " + strArrAux[29], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.PatientDeathIndicator_PID, enc.GetBytes(strArrAux[29])));
                 }
                 if ((strArrAux.Length > 30) && (strArrAux[30] != "") && (strArrAux[30] != null))     //  IdentityUnknownIndicator_PID=01535,
                 {
                     CLogger.Append("Field 30: " + strArrAux[30], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.IdentityUnknownIndicator_PID, enc.GetBytes(strArrAux[30])));
                 }
                 if ((strArrAux.Length > 31) && (strArrAux[31] != "") && (strArrAux[31] != null))     //   IdentityReliabilityCode_PID=01536,
                 {
                     CLogger.Append("Field 31: " + strArrAux[31], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.IdentityReliabilityCode_PID, enc.GetBytes(strArrAux[31])));
                 }
                 if ((strArrAux.Length > 32) && (strArrAux[32] != "") && (strArrAux[32] != null))     //   LastUpdateDateTime_PID=01537,
                 {
                     CLogger.Append("Field 32: " + strArrAux[32], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.LastUpdateDateTime_PID, enc.GetBytes(strArrAux[32])));
                 }

                 if ((strArrAux.Length > 33) && (strArrAux[33] != "") && (strArrAux[33] != null))     // LastUpdateFacility_PID=01538,
                 {
                     CLogger.Append("Field 33: " + strArrAux[33], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.LastUpdateFacility_PID, enc.GetBytes(strArrAux[33])));
                 }
                 if ((strArrAux.Length > 34) && (strArrAux[34] != "") && (strArrAux[34] != null))     //   SpeciesCode_PID=01539,
                 {
                     CLogger.Append("Field 34: " + strArrAux[34], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.SpeciesCode_PID, enc.GetBytes(strArrAux[34])));
                 }
                 if ((strArrAux.Length > 35) && (strArrAux[35] != "") && (strArrAux[35] != null))     //   BreedCode_PID=01540,
                 {
                     CLogger.Append("Field 35: " + strArrAux[35], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.BreedCode_PID, enc.GetBytes(strArrAux[35])));
                 }
                 if ((strArrAux.Length > 36) && (strArrAux[36] != "") && (strArrAux[36] != null))     //   Strain_PID=01541,
                 {
                     CLogger.Append("Field 36: " + strArrAux[36], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.Strain_PID, enc.GetBytes(strArrAux[36])));
                 }
                 if ((strArrAux.Length > 37) && (strArrAux[37] != "") && (strArrAux[37] != null))     //    ProductionClassCode_PID=01542,
                 {
                     CLogger.Append("Field 37: " + strArrAux[37], CLogger.INFO);
                     System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                     lstHL7Fields.Add(new HL7Fields(HL7FieldName.ProductionClassCode_PID, enc.GetBytes(strArrAux[37])));
                 }

             }
             catch (Exception ex)
             {                                  
                 throw new Exception("EXCEPCION AL DESCOMPONER SEGMENTO PID EX: "+ex.Message);
             }

            CLogger.Append("**********************FINAL DEL DESCOMPONER SEGMENTO PID OK*******************", CLogger.INFO);


              return lstHL7Fields;

        }
        #endregion
                     
        
        
        
       


    //    //public HL7SegmentPID()
    //    //    : base(HL7MessageName.MessagePID)
    //    //{

    //    //}

    //    //public int BuildFields(byte[] byteInputStreamParam)
    //    //{
    //    //    throw new NotImplementedException();
    //    //}

    //    //public int BuildFields(byte[] byteInputStreamParam, int intOffset)
    //    //{
    //    //    throw new NotImplementedException();
    //    //}
    }
}
